﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Abp;
using Abp.Dependency;
using Abp.Domain.Repositories;
using Abp.Domain.Uow;
using Abp.RemoteEventBus;
using Abp.Runtime.Caching;
using Castle.Core.Logging;
using FwProject.Authorization.Users;
using FwProject.Comm;
using FwProject.Core.Distribution;
using FwProject.Core.Restaurant;
using FwProject.Core.SettlementInfoModel;
using FwProject.Distribution;
using FwProject.Distribution.Dto;
using FwProject.PrePaymentInfo;
using FwProject.PublicService.JPush;
using FwProject.Restaurant;
using FwProject.Users;
using Microsoft.EntityFrameworkCore;

namespace FwProject.RemoteEventBus
{
    /// <summary>
    /// 根据回库的车辆数据自动生成日结的收据和收款
    /// </summary>

    [RemoteEventHandler(ForType = RemoteTypeHelp.TYPERESTAURANTTYPENNUMBER, ForTopic = RemoteTypeHelp.TOPICREMOTERESTAURANTTYPENNUMBERINFO)]
    public class RemoteRestaurantTypenNumberInfo : IRemoteEventHandler, ITransientDependency
    {
        public readonly ICacheManager _catchmanager;
        public readonly IJPushAppservice _jPushAppservice;
        public readonly IUserAppService _userAppService;
        public readonly IRepository<LoadCar, Guid> _repositoryLoadCar;//装车记录
        public readonly IRepository<RestaurantInfo, Guid> _repositoryRestaurantInfo;//餐厅信息
        public readonly IRepository<DistributionInfo, Guid> _repositoryDistributionInfo;//配送
        public readonly IRepository<DistributionInfoDetails, Guid> _repositoryDistributionInfoDetails;//配送明细
        public readonly IRepository<RestaurantTypenNumberInfo, Guid> _repositoryRestaurantTypenNumberInfo;//收据生成
        private readonly IRepository<User, long> _userRepository;
        public IGuidGenerator _guid { get; set; }
        public ILogger logging { get; set; }

        private readonly IUnitOfWorkManager _unitOfWorkManager;
        public RemoteRestaurantTypenNumberInfo(
                                          ICacheManager catchmanager, IJPushAppservice jPushAppservice,
                                          IRepository<LoadCar, Guid> repositoryLoadCar,
                                          IUserAppService userAppService,
                                          IRepository<User, long> userRepository,
                                          IRepository<RestaurantInfo, Guid> repositoryRestaurantInfo,
                                          IRepository<DistributionInfo, Guid> repositoryDistributionInfo,
                                          IRepository<DistributionInfoDetails, Guid> repositoryDistributionInfoDetails,
                                          IRepository<RestaurantTypenNumberInfo, Guid> repositoryRestaurantTypenNumberInfo,
                                          IUnitOfWorkManager unitOfWorkManager

                                          )
        {
            _repositoryRestaurantInfo = repositoryRestaurantInfo;
            _repositoryDistributionInfo = repositoryDistributionInfo;
            _repositoryDistributionInfoDetails = repositoryDistributionInfoDetails;
            _repositoryRestaurantTypenNumberInfo = repositoryRestaurantTypenNumberInfo;
            _userRepository = userRepository;
            _repositoryLoadCar = repositoryLoadCar;
            _catchmanager = catchmanager;
            _jPushAppservice = jPushAppservice;
            logging = NullLogger.Instance;
            _userAppService = userAppService;
            _unitOfWorkManager = unitOfWorkManager;

        }
        [UnitOfWork]
        public void HandleEvent(RemoteEventArgs eventArgs)
        {
            using (var unitOfWork = _unitOfWorkManager.Begin())
            {
                try
                {
                    ///餐厅绑定型号型号
                    var Id = Guid.Parse(eventArgs.EventData.Data[RemoteTypeHelp.MODELID].ToString());
                    ///得到餐厅绑定的型号
                    var model = _repositoryRestaurantTypenNumberInfo.GetAllIncluding(o => o.TypeNumberInfoModel).Where(o => o.Id == Id).FirstOrDefault();
                    if (model != null)
                    {
                        var list = _repositoryDistributionInfoDetails.GetAll().Where(o => o.Restaurantid == model.Restaurantid && o.TypeNumberInfoId == model.TypeNumberId && o.PricesId == Guid.Empty);
                        foreach (var item in list)
                        {
                            if(item.TypeNumberPrice==0){
                                ///重新结算确认订单单价
                                item.RestaurantTypenNumberInfoId = model.Id;
                                item.TypeNumberInfoId = model.TypeNumberInfoModel.Id;
                                item.TypeNumberName = model.TypeNumberInfoModel.Name;
                                item.TypeNumberPrice = model.Price;
                                item.Sumprice = model.Price * ((item.Distributioncount - item.Backboxcount) * model.TypeNumberInfoModel.Count - item.Backnumbercount - item.Damagecount);
                                //修改当前配送值
                                var m = _repositoryDistributionInfo.GetAllIncluding(o => o.DistributionInfoListDetails).Where(o => o.Id == item.DistributionInfoId).FirstOrDefault();
                                if (m != null)
                                {
                                    var price = m.DistributionInfoListDetails.Where(o => o.Id != item.Id).Sum(o => o.Sumprice);
                                    m.TotalPrice = price + item.Sumprice;
                                } 
                            }
                          

                        }
                    }

                }
                catch (Exception exe)
                {
                    logging.Info($"{nameof(RemoteRestaurantTypenNumberInfo)}{exe.ToString()}");

                }
                finally
                {
                    logging.Info($"{nameof(RemoteRestaurantTypenNumberInfo)}");
                }
                unitOfWork.Complete();
            }
        }
    }
}
